package com.market2.session;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.jboss.ejb3.annotation.RemoteBinding;

import com.market2.dto.SocieteDto;
import com.market2.model.Societe;
import com.market2.utils.EntityManagerThreadLocal;
import com.market2.utils.Globals;
import com.market2.utils.converter.ConverterUtil;

/**
 * Session Bean implementation class SocieteManager
 */

@Stateless @RemoteBinding(jndiBinding="Market2EAR/SocieteManager/remote")
public class SocieteManager implements SocieteManagerRemote
{
	@PersistenceContext(unitName=Globals.PERSISTENCE_UNIT_NAME)
	private EntityManager entityManager;
	
	@PostConstruct
	void init() {
		EntityManagerThreadLocal.set(entityManager);
	}

	@Override
	public List<SocieteDto> getSocietesByCriteria(Map<String, Object> props)
	{
		if(props == null) {
			return new ArrayList<SocieteDto>();
		}
		
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<Societe> criteriaQuery = criteriaBuilder.createQuery(Societe.class);
		Root<Societe> from = criteriaQuery.from(Societe.class);
		
		for(String prop : props.keySet())
		{
			Object val = props.get(prop);
			
			Path<Societe> param = from.get(prop);
			Predicate condition = criteriaBuilder.equal(param, val);
			criteriaQuery.where(criteriaBuilder.and(condition));
		}
		
		CriteriaQuery<Societe> select = criteriaQuery.select(from);
		TypedQuery<Societe> typedQuery = entityManager.createQuery(select);

		List<Societe> queryResult = typedQuery.getResultList();
		List<SocieteDto> result = ConverterUtil.convertTo(queryResult, SocieteDto.class);
		
		return result;
	}

	@Override
	public List<SocieteDto> getAllSocietes()
	{
		Query query = entityManager.createQuery("from Societe");
		
		@SuppressWarnings("unchecked")
		List<Societe> queryResult = query.getResultList();
		
		List<SocieteDto> result = ConverterUtil.convertTo(queryResult, SocieteDto.class);
		return result;
	}

	@Override
	public SocieteDto getSocieteByNom(String nom)
	{
		Societe societe = entityManager.find(Societe.class, nom);
		return ConverterUtil.convertTo(societe, SocieteDto.class);
	}

	@Override
	public void enregistrerSociete(SocieteDto societe)
	{
		Societe newSociete = ConverterUtil.convertFrom(societe, Societe.class);
		entityManager.persist(newSociete);
		entityManager.flush();
	}

	@Override
	public void accrediterSociete(String nomSociete)
	{
		Societe societe = entityManager.find(Societe.class, nomSociete);
		societe.setStatus(true);
	}
}
